
var app = getApp()
Page({

  data: {
    shareTempFilePath: "",
    backData: {},
    point1: '一维',
    point2: '二维',
    point3: '三维',
    point4: '四维',
    point5: '五维',
    remark: [],
    remark1:'',
    commen: '',
    tempcontext: null,
    temp1: '',
    temp2: '',
    temp3: '',
    temp4: '',
    temp5: '',
    tempCircle: '',
    createpath: '',
    screenWidth: '',
    screenHeight: '',
    pixelRatio: '',
    model: '',
    brand: '',
    count: 0,
    canvasHidden:true,
    max_weidu:'',
    temp_weidu:'',
    font_size:''
  },
  go: function () {
    wx.navigateTo({
      url: '../askDetail/askDetail',
    })
  },
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({
      success: res => {
        that.setData({
          screenWidth: res.screenWidth,
          screenHeight: res.screenHeight,
          pixelRatio: res.pixelRatio,
          brand: res.brand,
          model: res.model,
        })

      }
    })


    this.data.backData = JSON.parse(options.backData);

    var ss = this.data.backData;

    this.setData({
      point1: "一维:" + ss.weidu['1'] + "题",
      point2: "二维:" + ss.weidu['2'] + "题",
      point3: "三维:" + ss.weidu['3'] + "题",
      point4: "四维:" + ss.weidu['4'] + "题",
      point5: "五维:" + ss.weidu['5'] + "题",
      remark: ss.remark,
      commen: ss.commen,
      temp_weidu: ss.max_weidu,
      remark1: ss.remark[0],
      
    })

   var tempmax_weidu
    if (ss.max_weidu===1){
      tempmax_weidu = '一'
    } else if (ss.max_weidu === 2){
      tempmax_weidu = '二'
    } else if (ss.max_weidu === 3) {
      tempmax_weidu = '三'
    } else if (ss.max_weidu === 4) {
      tempmax_weidu = '四'
    } else if (ss.max_weidu === 5) {
      tempmax_weidu = '五'
    }
    this.setData({
      max_weidu: tempmax_weidu,
    
    })
    console.log(' console.log(ss.remark.length)='+ss.remark.length)
      // var tempsize  
      // if (ss.remark.length === 0|| ss.remark.length===1){
      //   tempsize=   28
      // }
      // else if (ss.remark.length === 2){
      //   tempsize = 26
      // } else if (ss.remark.length === 3) {
      //   tempsize = 24
      // } else if (ss.remark.length === 4) {
      //   tempsize = 22
      // }

      // this.setData({
      //   font_size: tempsize
      // })
     


    
    var context = wx.createCanvasContext('Canvas');
    var array = [ss.weidu['1'], ss.weidu['2'], ss.weidu['3'], ss.weidu['4'], ss.weidu['5']];
    var colors = ["#89e4ce", "#397269", "#3D8476", "#599A93", "#4ca893"];
    var total = 0;
    for (var val = 0; val < array.length; val++) {
      total += array[val];
    }
    var point = { x: 80, y: 80 };
    var radius = 80;
    for (var i = 0; i < array.length; i++) {
      context.beginPath();
      var start = 0;
      if (i > 0) {
        for (var j = 0; j < i; j++) {
          start += array[j] / total * 2 * Math.PI;
        }
      }
      context.arc(point.x, point.y, radius, start, start + array[i] / total * 2 * Math.PI, false);
      //  context.setLineWidth(2);
      context.lineTo(point.x, point.y);
      //  context.setStrokeStyle("#f5f5f5");
      context.setFillStyle(colors[i]);
      context.fill();

      context.closePath();
      //  context.stroke();
    }

    context.draw();
    this.createRec('rec1', 0, 10, "#89e4ce");
    this.createRec('rec2', 0, 10, "#397269");
    this.createRec('rec3', 0, 10, "#3D8476");
    this.createRec('rec4', 0, 10, "#599A93");
    this.createRec('rec5', 0, 10, "#4ca893");
  },


  createRec: function (id, x, y, colors) {
    var ctx = wx.createCanvasContext(id)
    ctx.rect(x, y, 15, 15)
    ctx.setFillStyle(colors)
    ctx.fill()
    ctx.draw()

  },

  gohome: function () {
    wx.switchTab({
      url: '../../index'
    })
  },

  downpic: function () {
    this.gosave();


  },


  gobook:function(){
    wx.navigateTo({
      url: '../newbook/newbook?weidu=' + this.data.temp_weidu,
    })
  },

  gosave: function () {
    var s;
    var that = this
    wx.canvasToTempFilePath({

      width: 360,
      height: 160,
      destWidth: 400,
      destHeight: 200,
      canvasId: 'Canvas',
      quality: 1,
      success: function (res) {
        s = res.tempFilePath
        that.data.count = that.data.count + 1;
        that.setData({
          tempCircle: res.tempFilePath,
          count: that.data.count
        })

      }, fail(e) {
        console.log(e)
      }
    })

    wx.canvasToTempFilePath({

      width: 15,
      height: 15,
      destWidth: 20,
      destHeight: 20,
      canvasId: 'rec1',
      quality: 1,
      success: function (res) {
        s = res.tempFilePath
        that.data.count = that.data.count + 1;
        that.setData({
          temp1: res.tempFilePath,
          count: that.data.count
        })
      }, fail(e) {
        console.log(e)
      }
    })


    wx.canvasToTempFilePath({

      width: 15,
      height: 15,
      destWidth: 20,
      destHeight: 20,
      canvasId: 'rec2',
      quality: 1,
      success: function (res) {
        s = res.tempFilePath
        that.data.count = that.data.count + 1;
        that.setData({
          temp2: res.tempFilePath,
          count: that.data.count
        })

      }, fail(e) {
        console.log(e)
      }
    })

    wx.canvasToTempFilePath({

      width: 15,
      height: 15,
      destWidth: 20,
      destHeight: 20,
      canvasId: 'rec3',
      quality: 1,
      success: function (res) {
        s = res.tempFilePath
        that.data.count = that.data.count + 1;
        that.setData({
          temp3: res.tempFilePath,
          count: that.data.count
        })
      
      }, fail(e) {
        console.log(e)
      }
    })
    wx.canvasToTempFilePath({

      width: 15,
      height: 15,
      destWidth: 20,
      destHeight: 20,
      canvasId: 'rec4',
      quality: 1,
      success: function (res) {
        s = res.tempFilePath
        that.data.count = that.data.count + 1;
        that.setData({
          temp4: res.tempFilePath,
          count: that.data.count
        })

      }, fail(e) {
        console.log(e)
      }
    })
    wx.canvasToTempFilePath({

      width: 15,
      height: 15,
      destWidth: 20,
      destHeight: 20,
      canvasId: 'rec5',
      quality: 1,
      success: function (res) {
        s = res.tempFilePath
        that.data.count = that.data.count + 1;
        that.setData({
          temp5: res.tempFilePath,
          count: that.data.count
        })

      }, fail(e) {
        console.log(e)
      }
    })
    setTimeout(function () {
      console.log(that.data.count)
      that.createaa()

    }, 1000)

  },

  createaa: function () {
    wx.showLoading({
      title: '保存中..',
    })
    this.setData({
      canvasHidden: false
    })

    var context = wx.createCanvasContext('mm');
    var uint = this.data.screenWidth / 750
    var uint2 = this.data.screenHeight / 1279



      context.drawImage('../../resource/ask_save.jpg', 0, 0, uint * 750, uint * 1279)
      if (this.data.screenWidth === 375 && this.data.screenHeight===812)
        context.drawImage(this.data.tempCircle, uint * 80, uint * 140, uint * 630, uint * 360)
      else
        context.drawImage(this.data.tempCircle, uint * 80, uint * 140, uint * 630, uint * 340)
     

      context.drawImage(this.data.temp1, uint * 450, uint * 140, uint * 25, uint * 70)
      context.drawImage(this.data.temp2, uint * 450, uint * 190, uint * 25, uint * 70)
      context.drawImage(this.data.temp3, uint * 450, uint * 240, uint * 25, uint * 70)
      context.drawImage(this.data.temp4, uint * 450, uint * 290, uint * 25, uint * 70)
      context.drawImage(this.data.temp5, uint * 450, uint * 340, uint * 25, uint * 70)


      context.setFontSize(uint *24)
    context.setFillStyle("#f3f4c8")

    context.fillText(this.data.point1, uint * 500, uint * 210)
    context.fillText(this.data.point2, uint * 500, uint * 255)
    context.fillText(this.data.point3, uint * 500, uint * 305)
    context.fillText(this.data.point4, uint * 500, uint * 355)
    context.fillText(this.data.point5, uint * 500, uint * 410)


    context.setFontSize(uint*28)
    var index = 0;
 


    var index = 0;
    for (var val = 0; val < this.data.remark.length; val++) {

      if (this.data.remark[val].length > 20) {
        context.fillText(this.data.remark[val].substring(0, 20),
          (uint * 750 - context.measureText(this.data.remark[val].substring(0, 20)).width) / 2,
          uint * (600 + 30 * index));

        index = index + 1;
        context.fillText(this.data.remark[val].substring(20, this.data.remark[val].length),
          (uint * 750 - context.measureText(this.data.remark[val].substring(20, this.data.remark[val].length)).width) / 2,
          uint * (600 + 30 * index));

      } else {
        context.fillText(this.data.remark[val], (uint * 750 - context.measureText(this.data.remark[val]).width) / 2,
          uint * (600 + 30 * index))
      }
      index = index + 1;
    }



    if (this.data.commen.length > 20) {

      context.fillText(this.data.commen.substring(0, 20),
        (uint * 750  - context.measureText(this.data.commen.substring(0, 20)).width) / 2,
        uint * 1279 - uint * (270 - 30 * index));

      index = index + 1;
      context.fillText(this.data.commen.substring(20, this.data.commen.length),
        (uint * 750 - context.measureText(this.data.commen.substring(20, this.data.commen.length)).width) / 2,
        uint * 1279 - uint * (270 - 30 * index));

    } else {
      context.fillText(this.data.commen, (uint * 750  - context.measureText(this.data.commen).width) / 2,
        uint * 1279 - uint * (270 - 30 * index))

    }


    context.draw();

    var that = this

    setTimeout(function () {
      wx.canvasToTempFilePath({

        width:  750,
        height: 1279,
        destWidth:  750,
        destHeight: 1279,
        canvasId: 'mm',
        quality: 1,
        success: function (res) {

          that.setData({
            createpath: res.tempFilePath
          })
          that.cc()
        }, fail(e) {
          console.log(e)
        }
      })
    }, 1000)

  },

  cc: function () {
    var that = this
    setTimeout(function () {
      wx.saveImageToPhotosAlbum({
        filePath: that.data.createpath,
        success: (res) => {
          wx.hideLoading()
          console.log(res)
          that.setData({
            canvasHidden: true
          })
        },
        fail: (err) => {
          console.log(err)
        }
      })
    }, 1000)
  },

  createbb: function (id) {
    var context = wx.createCanvasContext(id);

    context.drawImage(this.data.shareTempFilePath, 0, 0, 200, 150)
    context.draw();
  },



  gofriend: function () {

  },
  goshare: function () {
    wx.showShareMenu({
      withShareTicket: true
    })
  },

  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
   
      title: '我是' + this.data.max_weidu+'维的企业家，测测你是几维的？',
      path: 'pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

  onUnload: function () {
    wx.switchTab({
      url: '../../index'
    })
  },
})